package com.xiaoq.helper.repository.impl;

import java.util.ArrayList;
import java.util.List;

import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.xiaoq.helper.entity.HelperEntity;
import com.xiaoq.helper.repository.IHelperRepository;

@Repository
public class HelperRepositoryImpl implements IHelperRepository{
	
	@Autowired
	private Datastore datastore;

	@Override
	public boolean save(HelperEntity helper) {
		datastore.save(helper);
		return true;
	}

	@Override
	public List<HelperEntity> findHelper() {
		List<HelperEntity> helpers = new ArrayList<>();
		Query<HelperEntity> helperQuery = datastore.find(HelperEntity.class);
		helpers = helperQuery.asList();
		return helpers;
	}

	@Override
	public List<HelperEntity> findByQuery(String query) {
		List<HelperEntity> helpers = new ArrayList<>();
		Query<HelperEntity> helperQuery = datastore.find(HelperEntity.class);
		helperQuery.field("question").contains(query);
		helpers = helperQuery.asList();
		return helpers;
	}

}
